package com.breakfast.fun.ui.user.order;

import com.breakfast.fun.data.entity.mall.MallOrder;
import com.breakfast.fun.data.entity.user.recharge.RechargeMoney;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/30.
 */
public class MallOrderPresenter extends MallOrderContract.Presenter {
    @Override
    public void getMallOrder() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "list");
        map.put("begin", "0");
        map.put("end", "10");

        rxManager.add(model.getMallOrderModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<MallOrder>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.stopRefresh();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MallOrder mallOrder) {
                        view.stopRefresh();
                        if (mallOrder.isSuccess()) {
                            view.showMallOrder(mallOrder);
                        } else {
                            T.show(mallOrder.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void loadMallOrder(int begin) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "list");
        map.put("begin", begin + "");
        map.put("end", "10");

        rxManager.add(model.getMallOrderModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<MallOrder>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.stopRefresh();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MallOrder order) {
                        view.stopRefresh();
                        if (order.isSuccess()) {
                            view.loadBFOrder(order);
                        } else {
                            T.show(order.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getPayBean(String orderNo) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("orderNo", orderNo);
        map.put("orderType", "2");

        showLoadingDialog(false);
        rxManager.add(model.getPayBeanModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<RechargeMoney>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(RechargeMoney rechargeMoney) {
                        hideLoadingDialog();
                        if (rechargeMoney.isSuccess()) {
                            view.toPay(rechargeMoney.getData());
                        } else {
                            T.show(rechargeMoney.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void onStart() {
        getMallOrder();
    }
}
